package com.tcl.logoidentify.redis;

import jodd.util.ThreadUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.BinaryJedisPubSub;
import redis.clients.jedis.Jedis;

import com.tcl.logoidentify.commons.ByteUtils;
import com.tcl.logoidentify.commons.ConfigUtils;
import com.tcl.logoidentify.commons.FPCache;
import com.tcl.logoidentify.commons.FPCacheFactory;

public class FPSub {
	private static final Logger LOGGER = LoggerFactory.getLogger(FPSub.class);

	public static void start() {

		new Thread() {
			public void run() {
				while (true) {
					try {
						JedisTemplate.getInstance().getJedisPool().getResource().subscribe(new BinaryJedisPubSub() {
							@Override
							public void onMessage(byte[] channel, byte[] message) {
								process(channel, message);
							}

							private void process(byte[] channel, byte[] message) {
								int ch_id = ByteUtils.byte2Short(message, 12);
								if (message.length >25)
									FPCacheFactory.FPCACHE_88.getQueue(ch_id).add(message);
								else
									FPCacheFactory.FPCACHE_64.getQueue(ch_id).add(message);
							}

						}, "all_chs".getBytes());
					} catch (Exception e) {
						LOGGER.error("订阅采集卡指纹信息出错", e);
						ThreadUtil.sleep(1000);
					} finally {
					}
				}
			};
		}.start();
	}
}
